package com.cskaoyan.mall.search.service.impl;

import com.alibaba.fastjson.JSON;
import com.cskaoyan.mall.dto.AllItemResponse;
import com.cskaoyan.mall.dto.ItemDto;
import com.cskaoyan.mall.search.api.ProductApi;
import com.cskaoyan.mall.search.dto.SearchInitDto;
import com.cskaoyan.mall.search.entity.ItemDocument;
import com.cskaoyan.mall.search.service.InitDataService;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.IndicesClient;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

/**
 * @ClassName: InitDataServiceImpl
 * @Description:
 * @Author: 瞳
 * @Date: 2022/6/27 21:16
 */

@Service
public class InitDataServiceImpl implements InitDataService {

    @Autowired
    RestHighLevelClient client;

    @Autowired
    ProductApi productApi;

    @Override
    public void initItems() throws IOException {
        // 添加索引
        // 使用client获取操作索引对象
        IndicesClient indices = client.indices();
        // 设置索引名称
        CreateIndexRequest createIndexRequest = new CreateIndexRequest("product");
        // 设置映射
        String mapping = "{ \n" +
                "      \"properties\": { \n" +
                "        \"id\":{\"type\": \"long\"}, \n" +
                "        \"image\": {\"type\": \"text\"}, \n" +
                "        \"status\": {\"type\": \"long\"}, \n" +
                "        \"sellPoint\": { \n" +
                "          \"type\": \"text\",\n" +
                "          \"analyzer\": \"ik_max_word\" \n" +
                "        },\n" +
                "        \"title\": { \n" +
                "          \"type\": \"text\",\n" +
                "          \"analyzer\": \"ik_max_word\" \n" +
                "          \n" +
                "        },\n" +
                "        \"num\": {\"type\": \"long\"}, \n" +
                "        \"cid\": {\"type\": \"long\"}, \n" +
                "        \"created\": {\"type\": \"date\"}, \n" +
                "        \"price\": {\"type\": \"float\"}, \n" +
                "        \"limitNum\": {\"type\": \"long\"}, \n" +
                "        \"updated\": {\"type\": \"date\"} \n" +
                "      }\n" +
                "    }";

        // 创建索引
        createIndexRequest.mapping(mapping, XContentType.JSON);
        indices.create(createIndexRequest, RequestOptions.DEFAULT);

        // 批量导入数据
        // 获取所有商品
        AllItemResponse allProductItem = productApi.getAllProductItem();
        List<ItemDto> allItems = allProductItem.getAllItems();

        // bulk导入
        BulkRequest bulkRequest = new BulkRequest();

        // 循环创建IndexRequest添加数据
        for (ItemDto allItem : allItems) {
            SearchInitDto searchInitDto = new SearchInitDto();
            searchInitDto.setId(allItem.getId());
            searchInitDto.setTitle(allItem.getTitle());
            searchInitDto.setSellPoint(allItem.getSellPoint());
            searchInitDto.setPrice(allItem.getPrice());
            searchInitDto.setNum(allItem.getNum());
            searchInitDto.setLimitNum(allItem.getLimitNum());
            searchInitDto.setImage(allItem.getImage());
            searchInitDto.setCid(allItem.getCid());
            searchInitDto.setStatus(allItem.getStatus());
            searchInitDto.setCreated(allItem.getCreated());
            searchInitDto.setUpdated(allItem.getUpdated());

            String data = JSON.toJSONString(searchInitDto);
            IndexRequest indexRequest = new IndexRequest("product");
            indexRequest.id(searchInitDto.getId() + "");
            indexRequest.source(data, XContentType.JSON);
            bulkRequest.add(indexRequest);
        }

        // 执行批量处理
        BulkResponse bulkResponse = client.bulk(bulkRequest, RequestOptions.DEFAULT);
    }
}